<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Seo_handle extends MY_Controller
{
    function get_list()
    {
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 10 : $length;
        
        $this->load->model( 'seo_model' );
        $count = $this->seo_model->get_list_count();
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->seo_model->get_list( $length, $begin );
        $this->rs->ok( array (
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'seo_model' );
        if ($content = $this->seo_model->get( $id ))
        {
            $this->rs->ok( $content );
        }
        $this->rs->err( '内容不存在！' );
    }
    function modify()
    {
        $this->load->model( 'seo_model' );
        
        $id = ( int ) $this->input->post( 'id' );
        $is_insert = $id > 0 ? false : true;
        $modify_data = array ();
        
        $deleted = $this->input->post( 'deleted' );
        if ($is_insert || isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
        }
        
        $uri = $this->input->post( 'uri' );
        if ($is_insert || isset( $uri ))
        {
            $uri = substr_utf8( trim( $uri, ' /　' ), 255 );
            if (empty( $uri ))
            {
                $this->rs->err( '页面地址不能为空' );
            }
            $check_id = $this->seo_model->check_uri( $uri );
            if ($check_id && $check_id != $id)
            {
                $this->rs->err( '页面地址已存在！' );
            }
            $modify_data['uri'] = $uri;
        }
        
        $seo_title = $this->input->post( 'seo_title' );
        if ($is_insert || isset( $seo_title ))
        {
            if (empty( $seo_title ))
            {
                $this->rs->err( '标题不能为空' );
            }
            $modify_data['seo_title'] = substr_utf8( $seo_title, 50 );
        }
        
        $en_seo_title = $this->input->post( 'en_seo_title' );
        if (isset( $en_seo_title ))
        {
            $modify_data['en_seo_title'] = substr_utf8( $en_seo_title, 50 );
        }
        
        $seo_keywords = $this->input->post( 'seo_keywords' );
        if ($is_insert || isset( $seo_keywords ))
        {
            if (empty( $seo_keywords ))
            {
                $this->rs->err( '关键词不能为空' );
            }
            $modify_data['seo_keywords'] = substr_utf8( $seo_keywords, 255 );
        }
        
        $en_seo_keywords = $this->input->post( 'en_seo_keywords' );
        if (isset( $en_seo_keywords ))
        {
            $modify_data['en_seo_keywords'] = substr_utf8( $en_seo_keywords, 255 );
        }
        
        $seo_description = $this->input->post( 'seo_description' );
        if ($is_insert || isset( $seo_description ))
        {
            if (empty( $seo_description ))
            {
                $this->rs->err( '描述不能为空' );
            }
            $modify_data['seo_description'] = substr_utf8( $seo_description, 255 );
        }
        
        $en_seo_description = $this->input->post( 'en_seo_description' );
        if (isset( $en_seo_description ))
        {
            $modify_data['en_seo_description'] = substr_utf8( $en_seo_description, 255 );
        }
        
        $enable = $this->input->post( 'enable' );
        if ($is_insert || isset( $enable ))
        {
            $modify_data['enable'] = $enable ? 1 : 0;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        if ($is_insert)
        {
            if ($this->seo_model->insert( $modify_data ))
            {
                $this->rs->ok();
            }
        }
        else
        {
            if (false !== $this->seo_model->update( $modify_data, $id ))
            {
                $this->rs->ok();
            }
        }
        $this->rs->err();
    }
}